package com.agricultural.iframe;

import com.agricultural.bean.User;
import com.agricultural.dbchange.UserDB;
import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;

import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import javax.swing.text.StyleContext;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Locale;

public class ChangeUserInfo {
    private JPanel main;
    private JLabel title;
    private JPanel body;
    private JLabel inUsername;
    private JLabel username;
    private JTextField inPassword;
    private JTextField inMail;
    private JTextField inLocal;
    private JLabel password;
    private JLabel mail;
    private JLabel local;
    private JButton submit;
    private JCheckBox inIsAdmin;
    private JLabel isAdmin;
    private JTextField inUserNameTextField;
    private User user;
    private JFrame frame;

    public ChangeUserInfo(User changeUser) {
        checkAdmin(changeUser);
        inUsername.setText(user.getUsername());
        inUserNameTextField.setText(user.getUsername());
        inPassword.setText(user.getPassword());
        inMail.setText(user.getEmail());
        inLocal.setText(user.getAddress());
        inIsAdmin.setSelected(user.getIs_admin().equals("1"));
        initializeComponents();

        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                user.setUsername(inUserNameTextField.getText());
                user.setPassword(inPassword.getText());
                user.setEmail(inMail.getText());
                user.setAddress(inLocal.getText());
                user.setIs_admin(inIsAdmin.isSelected() ? "1" : "0");
                int res = UserDB.updateUser(user);
                if (res == 1) {
                    JOptionPane.showMessageDialog(null, "修改成功");
                } else {
                    JOptionPane.showMessageDialog(null, "修改失败");
                }
            }
        });
    }

    private void initializeComponents() {
        frame = new JFrame("ChangeUserInfo");
        frame.setSize(500, 400);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setContentPane(main);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.setVisible(true);
    }

    private void checkAdmin(User changeUser) {
        user = Login.user;
        if (user.getIs_admin().equals("1") && changeUser != null) {
            user = changeUser;
            inUserNameTextField.setVisible(true);
            inUsername.setVisible(false);
            isAdmin.setVisible(true);
            inIsAdmin.setVisible(true);
        } else {
            inUserNameTextField.setVisible(false);
            inUsername.setVisible(true);
            isAdmin.setVisible(false);
            inIsAdmin.setVisible(false);
        }
    }

    {
// GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<<
// DO NOT EDIT OR ADD ANY CODE HERE!
        $$$setupUI$$$();
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        main = new JPanel();
        main.setLayout(new FormLayout("fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        title = new JLabel();
        Font titleFont = this.$$$getFont$$$(null, -1, 22, title.getFont());
        if (titleFont != null) title.setFont(titleFont);
        title.setText("用户信息修改页面");
        CellConstraints cc = new CellConstraints();
        main.add(title, cc.xy(1, 1, CellConstraints.CENTER, CellConstraints.DEFAULT));
        body = new JPanel();
        body.setLayout(new GridLayoutManager(6, 3, new Insets(10, 10, 10, 10), -1, -1));
        main.add(body, cc.xy(1, 3));
        username = new JLabel();
        username.setText("用户名");
        body.add(username, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inUsername = new JLabel();
        inUsername.setText("Label");
        body.add(inUsername, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        password = new JLabel();
        password.setText("密码");
        body.add(password, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inPassword = new JTextField();
        body.add(inPassword, new GridConstraints(1, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        mail = new JLabel();
        mail.setText("邮箱");
        body.add(mail, new GridConstraints(2, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inMail = new JTextField();
        body.add(inMail, new GridConstraints(2, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        local = new JLabel();
        local.setText("邮寄地址");
        body.add(local, new GridConstraints(3, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inLocal = new JTextField();
        body.add(inLocal, new GridConstraints(3, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
        submit = new JButton();
        submit.setText("提交修改");
        body.add(submit, new GridConstraints(5, 1, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        isAdmin = new JLabel();
        isAdmin.setText("是否管理员");
        body.add(isAdmin, new GridConstraints(4, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inIsAdmin = new JCheckBox();
        inIsAdmin.setText("是就打对勾");
        body.add(inIsAdmin, new GridConstraints(4, 1, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
        inUserNameTextField = new JTextField();
        body.add(inUserNameTextField, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(150, -1), null, 0, false));
    }

    /**
     * @noinspection ALL
     */
    private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) {
        if (currentFont == null) return null;
        String resultName;
        if (fontName == null) {
            resultName = currentFont.getName();
        } else {
            Font testFont = new Font(fontName, Font.PLAIN, 10);
            if (testFont.canDisplay('a') && testFont.canDisplay('1')) {
                resultName = fontName;
            } else {
                resultName = currentFont.getName();
            }
        }
        Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize());
        boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac");
        Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize());
        return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback);
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return main;
    }

}
